//
//  XSJUITabBar.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/11.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit

protocol XSJTabBarDelegate: NSObjectProtocol {
    
    func didSelectedComposeButton()
}
class XSJUITabBar: UITabBar {
    
    
    var composeButtonClousre: (()->())?
    
    weak var XSJDelegate: XSJTabBarDelegate?
    
    private lazy var composeBut : UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.sizeToFit()
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
        
    }
    
    private func setupUI() {
        
        addSubview(composeBut)
        
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        composeBut.center.x = frame.size.width * 0.5
        composeBut.center.y = frame.size.height * 0.5
        
        let itmeWidth = self.frame.width / 5
        var index = 0
        for childView in subviews
        {
            
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                childView.frame.size.width = itmeWidth
                
                childView.frame.origin.x = CGFloat(index) * itmeWidth
                index++
                
                if index == 2 {
                    index++
                }
            }
            
//            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!)
//            {
//
//                childView.frame.size.width = itmeWidth
//                childView.frame.origin.x = CGFloat(index) * itemWidth
//                index++
//            }
        }
        
    }
    
    @objc private func composeButtonClick()
    {
        composeButtonClousre?()
        
        XSJDelegate?.didSelectedComposeButton()
    }
    
}
